package controllers

import (
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"news0/cache"
	"news0/models"
	"time"
)

type NewsController struct {
	beego.Controller
}

func (c *NewsController) Create() {
	var err error
	title := c.GetString("title")
	time := c.GetString("time")
	classify := c.GetString("classify")
	content := c.GetString("content")
	author := c.GetString("author")

	if title == "" || time == "" || classify == "" || content == "" || author == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	var news models.News
	news.Title = title
	news.Time = time
	news.Classify = classify
	news.Content = content
	news.Author = author

	if err = news.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	jsonDate, _ := json.Marshal(news)

	cache.Rdb.Set(cache.Ctx, "news_list", jsonDate, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
	}
	c.ServeJSON()
	return
}
func (c *NewsController) List() {
	//var err error
	keyword := c.GetString("keyword")
	typeId, _ := c.GetInt("type_id")
	Page, _ := c.GetInt("page")
	PageSize, _ := c.GetInt("page_size")

	key := fmt.Sprintf("news_list_%s_%d_%d", keyword, typeId, Page)
	//从缓存中获取
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	//if cache 存在，使用缓存
	//if cache 不存在，查询数据库，写入缓存
	var news models.News
	var list []*models.News

	if cacheData == "" {
		//从mysql数据库获取，缓存redis
		list, _ := news.List(keyword, Page, PageSize)
		//转换为json
		jsonData, _ := json.Marshal(list)
		//缓存数据
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24) //跟新缓存时间为一天
	} else {
		//从缓存中获取
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
		"data": list,
	}
	c.ServeJSON()
	return
}
func (c *NewsController) Up() {
	var err error
	id, _ := c.GetInt("id")
	title := c.GetString("title")
	time := c.GetString("time")
	classify := c.GetString("classify")
	content := c.GetString("content")
	author := c.GetString("author")

	if id == 0.0 || title == "" || time == "" || classify == "" || content == "" || author == "" {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "数据不能为空",
		}
		c.ServeJSON()
		return
	}

	var news models.News
	news.Title = title
	news.Time = time
	news.Classify = classify
	news.Content = content
	news.Author = author

	if err = news.Create(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	jsonDate, _ := json.Marshal(news)

	cache.Rdb.Set(cache.Ctx, "news_list", jsonDate, 0)
	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
	}
	c.ServeJSON()
	return
}
func (c *NewsController) Del() {
	var err error
	id, _ := c.GetInt("id")
	var news models.News
	news.ID = uint(id)

	if err = news.Del(); err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "系统错误",
		}
		c.ServeJSON()
		return
	}

	key := fmt.Sprintf("news_list_%d", news.ID)
	cache.Rdb.Del(cache.Ctx, key)

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "success",
	}
	c.ServeJSON()
	return

}
